var vue = new Vue({
    el : '#app',
    data : {
        tableData: [],
        loading: false,

        formVisible: false,
        formRead: false,
        queryForm:{
            current : 1,
            size : 10,
            total : 0,
        },
        userForm: {
            loginName : '',
            nickName  :'',
            telephone : '',
            remark : '',
            orgId : ''
        },
        formRules: {
            loginName: [
                {required: true, message: '请输入登录名称'},
            ],
            password: [
                {required: true, message: '请输入登录密码'},
            ],
            nickName: [
                {required: true, message: '请输入用户名'},
            ],
            telephone: [
                {required: true, message: '请输入手机号码'},
            ]
        },
    },
    created : function(){
        this.loadAllUser();
    },
    methods : {
        loadAllUser: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/user/list' ,
                data: JSON.stringify(self.queryForm),
                success: function (result) {
                    if (result.code === 200) {
                        self.tableData = result.body;
                        self.queryForm.total = result.total;
                    }
                }
            })
        },
        save : function(){
            var self = this;
            self.$refs['userForm'].validate(function (valid) {
                if (!valid) return false;

                $.ajax({
                    url: constant.ROOT_SITE + '/system/user/save',
                    data: JSON.stringify(self.userForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.formVisible = false;
                            self.loadAllUser();
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },
        //删除
        deleteUser : function(user){
            var self = this;
            if(user.userType === roleCode.COMPANY_ADMIN || user.userType === roleCode.SUPER_ADMIN){
                self.$message.error("管理员用户不可删除！");
                return
            }

            this.$confirm('您确定用删除该用户吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:  constant.ROOT_SITE + '/system/user/delete/' + user.id,
                    success: function (result) {
                        if (result.code === 200 && result.body) {
                            self.$message({
                                type: 'success',
                                message: '删除成功！'
                            });
                            self.loadAllUser();
                        }
                    }
                })
            });
        },
        updateBusinessStatus:function(row){
            var self = this;
            var form = {
                id : row.id,
                businessFlag:!row.businessFlag,
            }
            $.ajax({
                url: constant.ROOT_SITE + '/system/user/save',
                data: JSON.stringify(form),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '操作成功！'
                        });
                        row.businessFlag = !row.businessFlag;
                    } else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },
        handleSizeChange: function (pageSize) {
            this.queryForm.size = pageSize;
            this.loadAllUser();
        },
        handleCurrentChange: function (pageCurrent) {
            this.queryForm.current = pageCurrent;
            this.loadAllUser();
        },
        showDialog : function(){
            this.formVisible = true;
        },
        showEditDialog : function(user){
            $.extend(this.userForm, user);
            this.formVisible = true;
        },
        handleDialogClose :function () {
            this.userForm={
                loginName : '',
                nickName  :'',
                telephone : '',
                remark : '',
                orgId:''
            };
        }
    }
});